package com.project.erp.tag;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;
import com.project.erp.common.Constants;
import com.project.erp.model.system.Privilege;

public class PrivilegeTag extends BodyTagSupport {

	private static final long serialVersionUID = 8921470113192578477L;
	
	public PrivilegeTag(){
	}

	// 权限组uri
	protected String resourceUri;
	
	//是否有权限
	private boolean hasRight;
	
	@SuppressWarnings("unchecked")
	@Override
	public int doStartTag() throws JspException {
		
        //是否有权限默认值
		setHasRight(false);
		//用户拥有的权限
		List<Privilege> rightPrivilege = (List<Privilege>)pageContext.getSession().getAttribute(Constants.ERP_CURRENT_USER_RIGHTCODES);
		List<String> rightURL = new ArrayList<String>();
		for (Privilege privilege : rightPrivilege) {
			if(StringUtils.isNotBlank(privilege.getUrl())){
				rightURL.add(privilege.getUrl());
				if(StringUtils.isNotBlank(privilege.getExternal())){
					String[] urlArray = privilege.getExternal().split(Constants.PRIVILEGE_EXTERNAL_SPLIT);
					for (String url : urlArray) {
						rightURL.add(url);
					}
				}
			}
		}
		//根据传进来的uri判断用户是否有权限
		if(rightURL.contains(resourceUri)){
			hasRight = true;
		}
       
        return EVAL_BODY_INCLUDE;
	}
	
	public String getResourceUri() {
		return resourceUri;
	}

	public void setResourceUri(String resourceUri) {
		this.resourceUri = resourceUri;
	}

	public boolean isHasRight() {
		return hasRight;
	}

	public void setHasRight(boolean hasRight) {
		this.hasRight = hasRight;
	}
}
